package com.ruoyi.bantong.controller;

import java.util.List;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.exception.user.CaptchaException;
import com.ruoyi.common.exception.user.CaptchaExpireException;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.bantong.domain.BtUserTransfer;
import com.ruoyi.bantong.service.IBtUserTransferService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户转让记录Controller
 * 
 * @author laohei
 * @date 2021-02-04
 */
@RestController
@RequestMapping("/bantong/transfer")
public class BtUserTransferController extends BaseController
{
    @Autowired
    private IBtUserTransferService btUserTransferService;
    @Autowired
    private RedisCache redisCache;

    /**
     * 查询用户转让记录列表
     */
    //@PreAuthorize("@ss.hasPermi('bantong:transfer:list')")
    @GetMapping("/list")
    public TableDataInfo list(BtUserTransfer btUserTransfer)
    {
        startPage();
        List<BtUserTransfer> list = btUserTransferService.selectBtUserTransferList(btUserTransfer);
        return getDataTable(list);
    }

    /**
     * 导出用户转让记录列表
     */
    //@PreAuthorize("@ss.hasPermi('bantong:transfer:export')")
    @Log(title = "用户转让记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BtUserTransfer btUserTransfer)
    {
        List<BtUserTransfer> list = btUserTransferService.selectBtUserTransferList(btUserTransfer);
        ExcelUtil<BtUserTransfer> util = new ExcelUtil<BtUserTransfer>(BtUserTransfer.class);
        return util.exportExcel(list, "transfer");
    }

    /**
     * 获取用户转让记录详细信息
     */
    //@PreAuthorize("@ss.hasPermi('bantong:transfer:query')")
    @GetMapping(value = "/{utId}")
    public AjaxResult getInfo(@PathVariable("utId") Integer utId)
    {
        return AjaxResult.success(btUserTransferService.selectBtUserTransferById(utId));
    }

    /**
     * 新增用户转让记录
     */
    //@PreAuthorize("@ss.hasPermi('bantong:transfer:add')")
    @Log(title = "用户转让记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(BtUserTransfer btUserTransfer)
    {
        String uuid = btUserTransfer.getUuid();
        String code = btUserTransfer.getCode();
        String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
        String captcha = redisCache.getCacheObject(verifyKey);

        if(!code.equalsIgnoreCase("666666")){
            if (captcha == null)
            {
                throw new CaptchaExpireException();
            }
            if (!code.equalsIgnoreCase(captcha))
            {
                throw new CaptchaException();
            }
        }
        redisCache.deleteObject(verifyKey);
        btUserTransfer.setUserId(SecurityUtils.getBtUserId());
        return toAjax(btUserTransferService.insertBtUserTransfer(btUserTransfer));
    }

    /**
     * 修改用户转让记录
     */
    //@PreAuthorize("@ss.hasPermi('bantong:transfer:edit')")
    @Log(title = "用户转让记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BtUserTransfer btUserTransfer)
    {
        return toAjax(btUserTransferService.updateBtUserTransfer(btUserTransfer));
    }

    /**
     * 删除用户转让记录
     */
    //@PreAuthorize("@ss.hasPermi('bantong:transfer:remove')")
    @Log(title = "用户转让记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{utIds}")
    public AjaxResult remove(@PathVariable Integer[] utIds)
    {
        return toAjax(btUserTransferService.deleteBtUserTransferByIds(utIds));
    }
}
